
PLUG.Platform = PLUG_PLATFORM_SHARED

PLUG.ClassName = "sh_motd"
PLUG.PrintName = "MOTD Popup"
PLUG.Author = "thomasfn"

if (SERVER) then

	local MotdFile = "gmanage/motd.txt"
	local DefaultText = [[<div align="center"><b>The MOTD for this server has not yet been setup.</b><br>
		If you are an admin or a server owner, please set it up in the Server Management section.</div>]];
	
	function PLUG:Init()
		if (!file.Exists( MotdFile )) then file.Write( MotdFile, DefaultText ) end
		self.MOTD = file.Read( MotdFile )
	end
	
	function PLUG:SaveMotd()
		file.Write( MotdFile, self.MOTD or DefaultText )
	end

	function PLUG:AddHooks()
		plugs.Hook( PLUG_PLATFORM_SERVER, "PlayerSpawn" )
	end
	
	function PLUG:PlayerSpawn( ply )
		if (ply._MOTDSENT) then return end
		ply._MOTDSENT = true
		self:BeamMOTD( ply, true )
	end
	
	function PLUG:BeamMOTD( to, popup )
		datastream.StreamToClients( to, "gmanage", { "motd", self.MOTD, popup } )
	end
	
	function PLUG:DoConCommand( ply, com, args )
		if (com == "motd_see") then
			ply.NextMOTD = ply.NextMOTD or 0
			if (CurTime() >= ply.NextMOTD) then
				ply.NextMOTD = CurTime() + 5
				self:BeamMOTD( ply, true )
			end
			return true
		end
		if (com == "motd_get") then
			if (!ply:HasPriv( "manage_server" )) then return self:Err( ply ) end
			ply.NextMOTD = ply.NextMOTD or 0
			if (CurTime() >= ply.NextMOTD) then
				ply.NextMOTD = CurTime() + 5
				self:BeamMOTD( ply, false )
			end
			return true
		end
	end
	
	function PLUG:DataStreamSV( ply, tab )
		if (tab[1] == "motd_set") then
			if (!ply:HasPriv( "manage_server" )) then return self:Err( ply ) end
			self.MOTD = tab[2]
			self:SaveMotd()
			return true
		end
	end
	
	function PLUG:Err( ply, msg )
		ply:PrintMessage( HUD_PRINTTALK, msg or "You do not have enough privileges to do that!" )
		return true
	end

end

if (CLIENT) then

	local MOTDWIN

	function PLUG:DataStream( tab )
		if (tab[1] == "motd") then
			self.MOTD = tab[2]
			if (tab[3]) then
				self:ShowMOTD()
			end
			if (MOTDWIN && MOTDWIN:IsValid()) then
				plugs.Call( "BuildServerPanel", MOTDWIN, "motdcpanel" )
			end
		end
	end
	
	function PLUG:ShowMOTD()
		local m = self.MOTD
		if (!m) then return end
		
		local w, h = ScrW(), ScrH()
		
		local win = vgui.Create( "DFrame" )
		win:SetSize( w*0.8, h*0.8 )
		win:SetTitle( "GManage - Message of the Day" )
		win:SetDraggable( false )
		win:ShowCloseButton( false )
		win:Center()
		
		w, h = win:GetWide(), win:GetTall()
		
		function win:Paint()
			local w, h = self:GetWide(), self:GetTall()
			surface.SetDrawColor( color_black )
			surface.DrawRect( 0, 0, w, h )
			surface.SetDrawColor( color_white )
			surface.DrawOutlinedRect( 0, 0, w, h )
			surface.DrawLine( 0, 25, w, 25 )
		end
		
		local html = vgui.Create( "HTML", win ) -- It hurts me real bad to have to do this...
		html:SetPos( 5, 30 )
		html:SetSize( w-10, h-60 )
		html:SetHTML( m )
		
		local btn = vgui.Create( "DButton", win )
		btn:SetPos( w*0.3, h-28 )
		btn:SetSize( w*0.4, 25 )
		btn:SetText( "[ OK ]" )
		function btn:DoClick()
			win:Remove()
		end
		
		win:MakePopup()
	end
	
	local newaction = { { "motdcpanel", "MOTD Control" } }
	function PLUG:GetServerActions()
		return newaction
	end
	
	function PLUG:BuildServerPanel( window, acname )
		if (acname == "motdcpanel") then
			MOTDWIN = window
		else
			MOTDWIN = nil
		end
	end
	
	function PLUG:BuildActionPanel( pn, action )
		if (action == "motdcpanel") then
			local w, h = pn:GetWide(), pn:GetTall()
			
			local lbl = vgui.Create( "DLabel", pn )
			lbl:SetPos( 5, 5 )
			lbl:SetText( "The MOTD shown upon user join is below, in HTML." )
			lbl:SizeToContents()
			
			local txt = vgui.Create( "DTextEntry", pn )
			txt:SetPos( 5, 25 )
			txt:SetSize( w-10, h-60 )
			txt:SetMultiline( true )
			
			local btn = vgui.Create( "DButton", pn )
			btn:SetPos( 5, h-25 )
			btn:SetText( "Download MOTD" )
			btn:SetSize( w*0.5 - 10, 20 )
			function btn:DoClick() plugs.Call( "SendCommand", "motd_get" ) end
			
			local btnb = vgui.Create( "DButton", pn )
			btnb:SetPos( w*0.5 + 5, h-25 )
			btnb:SetText( "Apply Changes" )
			btnb:SetSize( w*0.5 - 10, 20 )
			function btnb:DoClick()
				datastream.StreamToServer( "gmanage_sv", { "motd_set", txt:GetValue() } )
			end
		end
	end

end